package com.example.oauth2demo.controller;

import com.example.oauth2demo.config.JwtTokenStoreConfig;
import io.jsonwebtoken.Jwts;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;

/**
 * description: 资源 <br>
 * date: 2021/5/22 0:02 <br>
 * author: xiehui <br>
 * version: 1.0 <br>
 */

@RestController
@RequestMapping("/user")
public class UserController {


    /*@RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication) {
        return authentication.getPrincipal();

    }*/

    @RequestMapping("/getCurrentUser")
    public Object getCurrentUser(Authentication authentication, @RequestHeader("Authorization") String authorization) {
        String token = authorization.substring(authorization.indexOf("Bearer") + 7);
        System.out.println("token:" + token);

        return Jwts.parser()
                .setSigningKey(JwtTokenStoreConfig.KEY.getBytes(StandardCharsets.UTF_8))
                .parseClaimsJws(token)
                .getBody();
    }
}
